//
//  BCRouteRequest.m
//  Pod
//
//  Created by Basic on 17/3/30.
//  Copyright © 2017年 green. All rights reserved.
//

#import "BCRouteRequest.h"
#import "BCRouteKitPrivate.h"
#import "BCRouteKitPublic.h"
#import <BCFoundation/NSDictionary+BCHelper.h>

@interface BCRouteRequest()
/// 所有参数，包括 parameter和 extData
@property (strong, nonatomic, readwrite) NSDictionary *allParams;
@end

@implementation BCRouteRequest
#pragma mark - system
- (instancetype) initWithURLStr:(NSString *)urlString
{
    self = [super init];
    if (self) {
        if (!urlString) {
            urlString = @"";
        }
        _urlString = urlString;
        [self bc_parseURL:urlString];
    }
    return self;
}

- (void)dealloc
{
#ifdef DEBUG
//    NSLog(@"BCRouteRequest dealloc");
#endif
}


#pragma mark - getter
-(NSDictionary *)allParams {
    if (!_allParams) {
        _allParams = @{};
    }
    return _allParams;
}
-(NSInteger)transitType {
    NSInteger transitType = [self.params[kBCRouteTable_TransitKey] integerValue];
    return transitType;
}
- (BOOL )rootPush {
    BOOL rootPush = [self.params[kBCRouteTable_RootPushKey] boolValue];
    return rootPush;
}
-(void)setExtData:(NSDictionary *)extData {
    _extData = extData;
    //保存所有的参数
    if (!_extData) {
        _allParams = self.params;
    } else {
        NSMutableDictionary *allParmasTmp = [[NSMutableDictionary alloc] init];
        if (self.params) {
            [allParmasTmp addEntriesFromDictionary:self.params];
        }
        [allParmasTmp addEntriesFromDictionary:_extData];
        _allParams = allParmasTmp;
    }
}

#pragma mark - helper

- (void)bc_parseURL:(NSString *)urlString
{
    if (urlString.length <=0) {
        return;
    }
    //url string 先encoding，再转URL
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
    if (url == nil) {
        return;
    }
    // 找到参数
    NSString *query = [url query];
    _params = [NSDictionary bc_dictionaryWithURLParams:query];
    // 找到path
    NSString *path = [url path];
    if ([path hasPrefix:@"/"]) {
        //去除第一个/
        path = [path substringFromIndex:1];
    }
    NSMutableArray *mPaths = [[path componentsSeparatedByString:@"/"] mutableCopy];
    // 从ios之后开始获取数据
    NSUInteger iosIndex = [mPaths indexOfObject:@"ios"];
    if (iosIndex == NSNotFound) {
        // do something
    }else{
        [mPaths removeObjectsInRange:NSMakeRange(0, iosIndex+1)];
    }
    if (mPaths.count > 2) {
        NSString *key = [[mPaths lastObject] copy];
        [mPaths removeLastObject];
        NSString *domain = [mPaths componentsJoinedByString:@"/"];
        _paths = @[domain,key];
        
    }else{
        _paths = [mPaths copy];
    }
}
@end
